home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / lang / intege~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  9.0 KB  |  316 lines

  1. /*
  2.  * @(#)Integer.java    1.26 95/10/04  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. /**
  23.  * The Integer class is a wrapper for integer values.  In Java, integers are not 
  24.  * objects and most of the Java utility classes require the use of objects.  Thus, 
  25.  * if you needed to store an integer in a hashtable, you would have to "wrap" an 
  26.  * Integer instance around it.
  27.  * @version     1.26, 10/04/95
  28.  * @author    Lee Boynton
  29.  * @author    Arthur van Hoff
  30.  */
  31. public final
  32. class Integer extends Number {
  33.     /**
  34.      * The minimum value an Integer can have.  The lowest minimum value an
  35.      * Integer can have is 0x80000000.
  36.      */
  37.     public static final int   MIN_VALUE = 0x80000000;
  38.  
  39.     /**
  40.      * The maximum value an Integer can have.  The greatest maximum value an
  41.      * Integer can have is 0x7fffffff.
  42.      */
  43.     public static final int   MAX_VALUE = 0x7fffffff;
  44.  
  45.     /**
  46.      * Returns a new String object representing the specified integer in
  47.      * the specified radix.
  48.      * @param i        the integer to be converted
  49.      * @param radix    the radix
  50.      * @see Character#MIN_RADIX
  51.      * @see Character#MAX_RADIX
  52.      */
  53.     public static String toString(int i, int radix) {
  54.     StringBuffer buf;
  55.     int digitCount = 0;
  56.     boolean minval = false;
  57.     boolean negative = false;
  58.     if (i == 0x80000000) {
  59.             switch (radix) {
  60.                 case 2:  return "-10000000000000000000000000000000";
  61.                 case 4:  return "-2000000000000000";
  62.                 case 8:  return "-20000000000";
  63.                 case 16: return "-80000000";
  64.                 default: 
  65.                     minval = negative = true;
  66.                     i = MAX_VALUE;
  67.             }
  68.     } else if (i < 0) {
  69.         i = -i;
  70.         negative = true;
  71.     }
  72.     buf = new StringBuffer(32);
  73.     while (i > 0) {
  74.         if (i < radix) {
  75.         buf.append(Character.forDigit(i,radix));
  76.         i = 0;
  77.         } else {
  78.         int j = i % radix;
  79.         i = i / radix;
  80.         buf.append(Character.forDigit(j,radix));
  81.         }
  82.         digitCount++;
  83.     }
  84.     if (digitCount > 0) {
  85.         int j = buf.length();
  86.         int k = 0;
  87.         char tmp[];
  88.         if (negative) {
  89.         tmp = new char[j + 1];
  90.         tmp[0] = '-';
  91.         k = 1;
  92.         } else {
  93.         tmp = new char[j];
  94.         }
  95.         i = 0;
  96.         while (j-- > 0) {
  97.         tmp[j+k] = buf.charAt(i++);
  98.         }
  99.         if (minval) {
  100.         tmp[tmp.length-1]++;
  101.         }
  102.         return String.valueOf(tmp);
  103.     } else
  104.         return "0";
  105.     }
  106.  
  107.     /**
  108.      * Returns a new String object representing the specified integer. The radix
  109.      * is assumed to be 10.
  110.      * @param i    the integer to be converted
  111.      */
  112.     public static String toString(int i) {
  113.     return toString(i,10);
  114.     }
  115.     
  116.     /**
  117.      * Assuming the specified String represents an integer, returns that integer's
  118.      * value. Throws an exception if the String cannot be parsed as an int.
  119.      * @param s        the String containing the integer
  120.      * @param radix     the radix to be used
  121.      * @exception    NumberFormatException If the String does not contain a parsable 
  122.      *                                        integer.
  123.      */
  124.     public static int parseInt(String s, int radix) throws NumberFormatException {
  125.         if (s == null) {
  126.             throw new NumberFormatException("null");
  127.         }
  128.     int result = 0;
  129.     boolean negative = false;
  130.     int i=0, max = s.length();
  131.     if (max > 0) {
  132.         if (s.charAt(0) == '-') {
  133.         negative = true;
  134.         i++;
  135.         }
  136.         while (i < max) {
  137.         int digit = Character.digit(s.charAt(i++),radix);
  138.         if (digit < 0)
  139.             throw new NumberFormatException(s);
  140.         result = result * radix + digit;
  141.         }
  142.     } else
  143.         throw new NumberFormatException(s);
  144.     if (negative)
  145.         return -result;
  146.     else
  147.         return result;
  148.     }
  149.  
  150.     /**
  151.      * Assuming the specified String represents an integer, returns that integer's
  152.      * value. Throws an exception if the String cannot be parsed as an int.
  153.      * The radix is assumed to be 10.
  154.      * @param s        the String containing the integer
  155.      * @exception    NumberFormatException If the string does not contain a parsable 
  156.      *                                        integer.
  157.      */
  158.     public static int parseInt(String s) throws NumberFormatException {
  159.     return parseInt(s,10);
  160.     }
  161.  
  162.     /**
  163.      * Assuming the specified String represents an integer, returns a new Integer
  164.      * object initialized to that value. Throws an exception if the String cannot be
  165.      * parsed as an int.
  166.      * @param s        the String containing the integer
  167.      * @param radix     the radix to be used
  168.      * @exception    NumberFormatException If the String does not contain a parsable 
  169.      *                                        integer.
  170.      */
  171.     public static Integer valueOf(String s, int radix) throws NumberFormatException {
  172.     return new Integer(parseInt(s,radix));
  173.     }
  174.  
  175.     /**
  176.      * Assuming the specified String represents an integer, returns a new Integer
  177.      * object initialized to that value. Throws an exception if the String cannot be
  178.      * parsed as an int. The radix is assumed to be 10.
  179.      * @param s        the String containing the integer
  180.      * @exception    NumberFormatException If the String does not contain a parsable 
  181.      *                                        integer.
  182.      */
  183.     public static Integer valueOf(String s) throws NumberFormatException
  184.     {
  185.     return new Integer(parseInt(s, 10));
  186.     }
  187.  
  188.     /**
  189.      * The value of the Integer.
  190.      */
  191.     private int value;
  192.  
  193.     /**
  194.      * Constructs an Integer object initialized to the specified int value.
  195.      * @param value    the initial value of the Integer
  196.      */
  197.     public Integer(int value) {
  198.     this.value = value;
  199.     }
  200.  
  201.     /**
  202.      * Constructs an Integer object initialized to the value specified by the
  203.      * String parameter.  The radix is assumed to be 10.
  204.      * @param s        the String to be converted to an Integer
  205.      * @exception    NumberFormatException If the String does not contain a parsable 
  206.      *                                        integer.
  207.      */
  208.     public Integer(String s) throws NumberFormatException {
  209.     this.value = parseInt(s, 10);
  210.     }
  211.  
  212.     /**
  213.      * Returns the value of this Integer as an int.
  214.      */
  215.     public int intValue() {
  216.     return value;
  217.     }
  218.  
  219.     /**
  220.      * Returns the value of this Integer as a long.
  221.      */
  222.     public long longValue() {
  223.     return (long)value;
  224.     }
  225.  
  226.     /**
  227.      * Returns the value of this Integer as a float.
  228.      */
  229.     public float floatValue() {
  230.     return (float)value;
  231.     }
  232.  
  233.     /**
  234.      * Returns the value of this Integer as a double.
  235.      */
  236.     public double doubleValue() {
  237.     return (double)value;
  238.     }
  239.  
  240.     /**
  241.      * Returns a String object representing this Integer's value.
  242.      */
  243.     public String toString() {
  244.     return String.valueOf(value);
  245.     }
  246.  
  247.     /**
  248.      * Returns a hashcode for this Integer.
  249.      */
  250.     public int hashCode() {
  251.     return value;
  252.     }
  253.  
  254.     /**
  255.      * Compares this object to the specified object.
  256.      * @param obj    the object to compare with
  257.      * @return         true if the objects are the same; false otherwise.
  258.      */
  259.     public boolean equals(Object obj) {
  260.     if ((obj != null) && (obj instanceof Integer)) {
  261.         return value == ((Integer)obj).intValue();
  262.     }
  263.     return false;
  264.     }
  265.  
  266.     /**
  267.      * Gets an Integer property. If the property does not
  268.      * exist, it will return 0.
  269.      * @param nm the property name
  270.      */
  271.     public static Integer getInteger(String nm) {
  272.     return getInteger(nm, null);
  273.     }
  274.  
  275.     /**
  276.      * Gets an Integer property. If the property does not
  277.      * exist, it will return val. Deals with Hexadecimal
  278.      * and octal numbers.
  279.      * @param nm the String name
  280.      * @param val the Integer value
  281.      */
  282.     public static Integer getInteger(String nm, int val) {
  283.         Integer result = getInteger(nm, null);
  284.         return (result == null) ? new Integer(val) : result;
  285.     }
  286.  
  287.     /**
  288.      * Gets an Integer property. If the property does not
  289.      * exist, it will return val. Deals with Hexadecimal
  290.      * and octal numbers.
  291.      * @param nm the property name
  292.      * @param val the integer value
  293.      */
  294.     public static Integer getInteger(String nm, Integer val) {
  295.     String v = System.getProperty(nm);
  296.     if (v != null) {
  297.         try {
  298.         if (v.startsWith("0x")) {
  299.             return Integer.valueOf(v.substring(2), 16);
  300.         }
  301.         if (v.startsWith("#")) {
  302.             return Integer.valueOf(v.substring(1), 16);
  303.         }
  304.         if (v.startsWith("0")) {
  305.             return Integer.valueOf(v.substring(1), 8);
  306.         }
  307.         return Integer.valueOf(v);
  308.         } catch (NumberFormatException e) {
  309.         }
  310.     }    
  311.     return val;
  312.     }
  313. }
  314.  
  315.  
  316.